package com.yzq.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yzq.annotation.AuthCheck;
import com.yzq.common.Result;
import com.yzq.constant.UserConstant;
import com.yzq.model.dto.EmailRegisterRequest;
import com.yzq.model.dto.PhoneRegisterRequest;
import com.yzq.model.dto.UserLoginRequest;
import com.yzq.model.dto.user.UserAddRequest;
import com.yzq.model.dto.user.UserDeleteRequest;
import com.yzq.model.dto.user.UserQueryRequest;
import com.yzq.model.dto.user.UserUpdateRequest;
import com.yzq.model.entity.User;
import com.yzq.model.vo.UserVO;
import com.yzq.service.PictureService;
import com.yzq.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;   

@Api(tags = "用户管理")
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    @Lazy
    private PictureService pictureService;

    /**
     * 邮箱注册
     * @param emailRegisterRequest 请求体
     * @return 用户id
     */
    @ApiOperation("邮箱注册")
    @PostMapping("/email/register")
    public Result<Long> emailRegister(@RequestBody EmailRegisterRequest emailRegisterRequest) {
        log.info("邮箱注册开始: {}", emailRegisterRequest);
        return Result.success(userService.emailRegister(emailRegisterRequest));
    }

    /**
     * 手机注册
     * @param phoneRegisterRequest 请求体
     * @return 用户端id
     */
    @ApiOperation("手机注册")
    @PostMapping("/phone/register")
    public Result<Long> phoneRegister(@RequestBody PhoneRegisterRequest phoneRegisterRequest) {
        log.info("手机注册开始: {}", phoneRegisterRequest);
        return Result.success(userService.phoneRegister(phoneRegisterRequest));
    }

    /**
     * 用户登录
     * @param userLoginRequest 登录请求体
     * @param request 请求
     * @return 用户信息
     */
    @ApiOperation("用户登录")
    @PostMapping("/login")
    public Result<UserVO> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
        log.info("用户登录开始: {}", userLoginRequest);
        return Result.success(userService.userLogin(userLoginRequest, request));
    }

    /**
     * 获取当前用户信息
     * @param request 请求
     * @return 用户信息
     */
    @ApiOperation("获取当前用户信息")
    @GetMapping("/getCurrentUser")
    public Result<UserVO> getCurrentUser(HttpServletRequest request) {
        log.info("获取当前用户信息开始");
        return Result.success(userService.getCurrentUser(request));
    }

    /**
     * 用户登出
     * @param request 登出请求
     * @return 登出结果
     */
    @ApiOperation("用户登出")
    @PostMapping("/logout")
    public Result<Boolean> userLogout(HttpServletRequest request) {
        log.info("用户登出开始");
        return Result.success(userService.userLogout(request));
    }

    /**
     * 获取用户列表
     * @param userQueryRequest 查询请求
     * @return 用户列表
     */
    @ApiOperation("获取用户列表")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @GetMapping("/list")
    public Result<Page<User>> getUserList(UserQueryRequest userQueryRequest) {
        log.info("获取用户列表开始: {}", userQueryRequest);
        return Result.success(userService.getUserList(userQueryRequest));
    }

    /**
     * 添加用户
     * @param userAddRequest 添加请求
     * @return 添加结果
     */
    @ApiOperation("添加用户")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @PostMapping("/add")
    public Result<Long> addUser(@RequestBody UserAddRequest userAddRequest) {
        log.info("添加用户开始: {}", userAddRequest);
        return Result.success(userService.addUser(userAddRequest));
    }

    /**
     * 删除用户
     * @param userDeleteRequest 删除请求
     * @return 删除结果
     */
    @ApiOperation("删除用户")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @DeleteMapping("/delete")
    public Result<Boolean> deleteUser(@RequestBody UserDeleteRequest userDeleteRequest) {
        log.info("删除用户开始: {}", userDeleteRequest);
        return Result.success(userService.deleteUser(userDeleteRequest));
    }

    /**
     * 修改用户
     * @param userUpdateRequest 修改请求
     * @return 修改结果
     */
    @ApiOperation("修改用户")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @PutMapping("/update")
    public Result<Boolean> updateUser(@RequestBody UserUpdateRequest userUpdateRequest) {
        log.info("修改用户开始: {}", userUpdateRequest);
        return Result.success(userService.updateUser(userUpdateRequest));
    }

    /**
     * 获取用户信息
     * @param id 用户id
     * @return 用户信息
     */
    @ApiOperation("获取用户信息")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @GetMapping("/get")
    public Result<User> getUserById(Long id) {
        log.info("获取用户信息开始: {}", id);
        return Result.success(userService.getUserById(id));
    }


    /**
     * 修改用户头像
     */
    @ApiOperation("修改用户头像")
    @PutMapping("/changeAvatar")
    public Result<String> changeUserAvatar(@RequestPart("file") MultipartFile file, HttpServletRequest request) throws Exception {
        log.info("修改用户头像开始: {}", file);
        pictureService.changeUserAvatar(file, request);
        return Result.success("修改成功");
    }

    /**
     * 用户修改本人信息
     */
    @ApiOperation("用户修改本人信息")
    @PutMapping("/updateCurrentUser")
    public Result<String> updateCurrentUser(@RequestBody UserUpdateRequest userUpdateRequest, HttpServletRequest request) {
        log.info("用户修改本人信息开始: {}", userUpdateRequest);
        return userService.updateCurrentUser(userUpdateRequest, request);
    }
}
